package mge
{
  import com.bit101.components.ProgressBar;
  import flash.events.Event;
  import flash.events.IOErrorEvent;
  import flash.events.ProgressEvent;
  import flash.net.FileFilter;
  import flash.net.FileReference;
  import flash.utils.ByteArray;
  import flash.utils.Dictionary;
  import flash.xml.*;

  public class Localization
  {
    public static const LANGUAGES:Array = new Array("en", "es");
    public static const MOODS:Array = new Array("normal", "happy", "sad", "surprised", "angry", "kissie", "satisfied", "sexy", "tired", "worried");
    
    public function Localization(xml:XML, language:String)
    {
      this.xml = xml;
      this.language = language;
      buildDic();
    }
    
    public function translate(tag:String):String
    {
      if (dic[tag] != null)
        return dic[tag].text[language]; 
      else
        return "#" + tag;
    }
    public function translateEx(tag:String):LocalizationResult { return dic[tag]; }
    
    public function set language(val:String):void { _language = val; }
    public function get language():String { return _language; }
    
    private function buildDic():void
    {
      dic = new Dictionary();
      
      if (!xml) return;
      
      var results:XMLList = xml.child("*");
      var item:XML;
      var lang:String;
      
      tags.splice(0, tags.length);
      
      for each (item in results) 
      {
        addTag((String)(item.@id), item);
      }
    }
    
    public function addTag(tag:String, item:XML = null):void
    {
      var lang:String;
      var lr:LocalizationResult = new LocalizationResult();
        
      lr.mood = item ? item.mood : "";
      
      for each (lang in LANGUAGES)
        lr.text[lang] = item ? item[lang] : "";
        
      dic[tag] = lr;
      tags.push(tag);
    }
    
    public function removeTag(tag:String):void
    {
      delete dic[tag];
      for (var i:int = 0; i < tags.length; i++)
        if (tags[i] == tag)
        {
          tags.splice(i, 1);
          return;
        }
    }
    
    
    public function export():void
    {
      var text:String = "<localization>";

      for (var i:int = 0; i < tags.length; i++)
      {
        
        var dialog:String = 
        '<dialog id="' + tags[i] + '">' + 
          '<mood>' + dic[tags[i]].mood + '</mood>';
          for (var j:int = 0; j < LANGUAGES.length; j++)
          {
            dialog += '<' + LANGUAGES[j] + '>';
            dialog += '<![CDATA[';
            dialog += dic[tags[i]].text[LANGUAGES[j]] + ']]>';
            dialog += '</' + LANGUAGES[j] + '>';
          }
        dialog += '</dialog>';
        
        text += dialog;
      }
      
      text += "</localization>";
      
      var _xml:XML = new XML(text);
      save(_xml);
    }
    
    // Opens a dialog to save file.
    private function save(_xml:XML):void
    {
      var ba:ByteArray = new ByteArray();
      ba.writeUTFBytes(_xml);
      var fr:FileReference = new FileReference();
      fr.save(ba, "localization.xml");
    }
    
    public function load(loaded:Function):void
    {
      this.loaded = loaded;
      file = new FileReference();
      file.browse(new Array(new FileFilter("XML files (*.xml)", "*.xml"), new FileFilter("All (*.*)", "*.*")));
      file.addEventListener(Event.SELECT, _selectFile);
    }
    
    private function _selectFile(e:Event):void
    {
      file.addEventListener(Event.COMPLETE, _loadFile);
      file.load();
    }
    
    private function _loadFile(e:Event):void
    {
      xml = new XML(file.data);
      buildDic();
      if (loaded != null) loaded(e);
      
      file = null;
      loaded = null;
    }
    
    private var _language:String;
    private var xml:XML;
    public var dic:Dictionary;
    public var tags:Vector.<String> = new Vector.<String>();
    private var file:FileReference;
    private var loaded:Function;

  }
}